package com.lsfb.testgamedemo.Activity;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;
import com.loopj.android.http.RequestParams;
import com.lsfb.testgamedemo.LoginBean;
import com.lsfb.testgamedemo.Utils.AsynHttp;
import com.lsfb.testgamedemo.Utils.CommonValue;
import com.lsfb.testgamedemo.Utils.NetCallBack;
import com.lsfb.testgamedemo.Utils.URLString;
import com.lsfbandroid.helloplugin.R;

import org.xutils.common.util.MD5;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import org.xutils.x;

@ContentView(R.layout.act_charge)
public class ChargeActivity extends Activity {

    private String TAG = "hello_help";

    @ViewInject(R.id.et_chargepwd)
    private EditText et_charge;

    @ViewInject(R.id.postcharge)
    private Button btn_post;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);

        btn_post.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (et_charge.getText().toString() == null || et_charge.getText().toString().isEmpty()) {
                    return;
                }
                if (CommonValue.uid.equals("-1")) {
                    Toast.makeText(x.app(), "用户异常,请重启应用后充值", Toast.LENGTH_LONG).show();
                    ChargeActivity.this.finish();
                    return;
                }
                RequestParams requestParams = new RequestParams();
                requestParams.add("userid", CommonValue.uid);
                requestParams.add("timecard", et_charge.getText().toString());
                requestParams.add("check", MD5.md5(CommonValue.uid + "androidhello"));
                Log.e(TAG, "提交参数:" + requestParams.toString());
                new AsynHttp().sendPost(URLString.CHARGE, requestParams, new NetCallBack() {
                    @Override
                    public void Success(String arg) {
                        Log.e(TAG, arg);
                        LoginBean bean = new Gson().fromJson(arg, LoginBean.class);
                        switch (bean.getCode()) {
                            case 200:
                                Toast.makeText(x.app(), "充值成功", Toast.LENGTH_LONG).show();
                                CommonValue.endtime = bean.getData();
                                ChargeActivity.this.finish();
                                break;
                            default:
                                Toast.makeText(x.app(), "充值失败", Toast.LENGTH_LONG).show();
                                break;
                        }
                    }

                    @Override
                    public void Failure(Throwable arg) {
                        Log.e(TAG, "充值失败,网络异常");
                        Toast.makeText(x.app(), "充值失败,网络异常", Toast.LENGTH_LONG).show();
                    }
                });
            }
        });

    }
}
